package cn.hd.userService.controller.user;

import cn.hd.commonService.vo.Query;
import cn.hd.commonService.vo.Result;
import cn.hd.commonService.annotations.LogAnnotation;
import cn.hd.commonService.entity.SysUser;
import cn.hd.commonService.enums.MethodType;
import cn.hd.userService.service.user.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {

    @Autowired
    private ISysUserService userService;

    @PostMapping("/register")
    @ApiOperation(value = "注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account",value = "账号",required = true),
            @ApiImplicitParam(name = "password",value = "密码",required = true),
            @ApiImplicitParam(name = "email",value = "邮箱",required = true),
    })
    @LogAnnotation(methodName = "注册",methodDesc = "用户注册",methodType = MethodType.POST_TYPE)
    public Result register(SysUser user){
        Assert.notNull(user,"用户信息有误");
        Assert.hasText(user.getAccount(),"账号不能为空");
        Assert.hasText(user.getEmail(),"邮箱不能为空");
        Assert.hasText(user.getPhone(),"电话不能为空");
        userService.register(user);
        return   Result.success("注册成功");
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordId",value = "用户id",required = true),
            @ApiImplicitParam(name = "phone",value = "电话",required = true),
            @ApiImplicitParam(name = "email",value = "邮箱",required = true),
    })
    @LogAnnotation(methodName = "修改用户",methodDesc = "修改用户",methodType = MethodType.PUT_TYPE)
    public Result update(SysUser user){
        Assert.notNull(user,"用户信息有误");
        Assert.notNull(user.getRecordId(),"用户id不能为空");
        Assert.hasText(user.getPhone(),"电话不能为空");
        Assert.hasText(user.getEmail(),"邮箱不能为空");
        userService.update(user);
        return   Result.success("修改成功");
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userIds",value = "用户id，以逗号分割",required = true)
    })
    @LogAnnotation(methodName = "删除用户信息",methodDesc = "删除用户信息",methodType = MethodType.DELETE_TYPE)
    public Result delete(String userIds){
        Assert.hasText(userIds,"用户id不能为空");
        userService.deleteBatch(userIds);
        return   Result.success("删除成功");
    }

    @PostMapping("/getListByPage")
    @ApiOperation(value = "分页获取用户的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "condition",value = "根据用户名或账号",required = false)
    })
    @LogAnnotation(methodName = "分页获取用户的信息",methodDesc = "分页获取用户的信息",methodType = MethodType.FIND_TYPE)
    public Result getListByPage(Query query,String condition){
        return   Result.success("获取成功",userService.getListByPage(query,condition));
    }

    @PostMapping("/getUserByAccount")
    @ApiOperation(value = "根据账号获取用户名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account",value = "用户账号",required = true)
    })
    @LogAnnotation(methodName = "根据账号获取用户名称",methodDesc = "根据账号获取用户名称",methodType = MethodType.FIND_TYPE)
    public Result getUserByAccount(String account){
        Assert.hasText(account,"用户账号不能为空");
        return   Result.success("获取成功",userService.getUserByAccount(account));
    }

    @PostMapping("/enableUser")
    @ApiOperation(value = "启用或禁用用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true),
            @ApiImplicitParam(name = "enable",value = "是否启用，1：启用，0：禁用，默认为1")
    })
    @LogAnnotation(methodName = "启用或禁用用户",methodDesc = "启用或禁用用户",methodType = MethodType.FIND_TYPE)
    public Result enableUser(Integer userId,@RequestParam(defaultValue = "1") Integer enable){
        Assert.notNull(userId,"用户id不能为空");
        userService.enableUser(userId,enable);
        return   Result.success("操作成功");
    }
    @PostMapping("/updatePassword")
    @ApiOperation(value = "修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account",value = "账号",required = true),
            @ApiImplicitParam(name = "password",value = "密码",required = true),
            @ApiImplicitParam(name = "recordId",value = "用户id",required = true)
    })
    @LogAnnotation(methodName = "修改密码",methodDesc = "修改密码",methodType = MethodType.PUT_TYPE)
    public Result updatePassword(String account,String password,Integer  recordId){
        Assert.notNull(recordId,"用户id不能为空");
        Assert.hasText(account,"账号不能为空");
        Assert.hasText(password,"密码不能为空");
        userService.updatePassword(recordId,account,password);
        return   Result.success("修改成功");
    }

    @PostMapping("/getUserByUserId")
    @ApiOperation(value = "根据用户id获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordId",value = "用户id",required = true),
    })
    @LogAnnotation(methodName = "根据用户id获取用户信息",methodDesc = "根据用户id获取用户信息",methodType = MethodType.FIND_TYPE)
    public Result getUserByUserId(Integer  recordId){
        Assert.notNull(recordId,"用户id不能为空");
        return   Result.success("获取成功",userService.getUserByUserId(recordId));
    }

    @PostMapping("/uploadPhoto")
    @ApiOperation(value = "上传头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordId",value = "用户id",required = true),
            @ApiImplicitParam(name = "photo",value = "用户头像",required = true,dataType ="file" )
    })
    @LogAnnotation(methodName = "根据用户id获取用户信息",methodDesc = "根据用户id获取用户信息",methodType = MethodType.PUT_TYPE)
    public Result uploadPhoto(@RequestParam(value = "photo") MultipartFile photo, Integer  recordId) throws Exception{
        Assert.notNull(recordId,"用户id不能为空");
        Assert.notNull(photo,"头像不能为空");
        userService.uploadPhoto(recordId,photo);
        return   Result.success("上传成功");
    }

}
